$(function () {
    $("#jqGrid").jqGrid({
        url: '../interview/getInfoBystatus',
        datatype: "json",
		postData: {"interviewStatus":"firstInterview"},
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{ label: '需求编号', name: 'damandid', index: 'damandid' ,hidden: true},
            { label: '求职单位名称', name: 'company', index: 'company' },
            { label: '岗位名称', name: 'jobname', index: 'jobname' },
			{ label: '求职人员姓名', name: 'empname', index: 'empname' },
			{ label: '性别', name: 'sex', index: 'sex',formatter:getDictInfo },
			{ label: '年龄', name: 'age', index: 'age' },
            { label: '联系电话', name: 'telphone', index: 'telphone' },
			{ label: '学历', name: 'education', index: 'education',formatter:getDictInfo },
			{ label: '证书', name: 'ertificate', index: 'ertificate' },
			{ label: '婚否', name: 'married', index: 'married',
                formatter:function(v){
                    if(v=="0"){
                        return "未婚";
                    }else if(v=="1"){
                        return "已婚";
                    }else if(v=="2"){
                        return "离异";
                    }else{
                        return v;
                    }
                } }, //(0,未婚，1，已婚)
			{ label: '求职单位编号', name: 'companyid', index: 'companyid',hidden: true },
			{ label: '岗位编号', name: 'jobid', index: 'jobid',hidden: true },
			{ label: '面试结果', name: 'firInterviewStatus', index: 'fir_interview_status',
                formatter:function(v){
                    if(v=="0"){
                        return "未开始";
                    }else if(v=="1"){
                        return "合格";
                    }else if(v=="2"){
                        return "不合格";
                    }else{
                        return v;
                    }
                } }, 	//(0,还未开始,1,通过一面,2,未通过)
			{ label: '二面状态', name: 'secInterviewStatus', index: 'sec_interview_status' , hidden: true}, 	//(0,还未开始,1,通过一面,2,未通过)
			{ label: '体检状态', name: 'physicalExaminationStatus', index: 'physical_examination_status', hidden: true},
			{ label: '推迟体检状态', name: 'laterPeTime', index: 'later_pe_time', hidden: true }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        //字段太多自动加载横轴滚动条
        shrinkToFit:false,
        autoScroll: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
            //doResize();
            //显示grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
        }
    });
});

//自动跳转列表宽度
/*function doResize(){
    var td=$('#tdCompute')//获取计算实际列长度的容器
        ,tds//临时保存列
        ,arr=[];//用于保存最大的列宽
    //遍历每行获得每行中的最大列宽
    $('#gview_jqGrid .ui-jqgrid-htable tr,#jqGrid tr:gt(0)').each(function(){
        $(this).find('td,th').each(function(idx){
            arr[idx]=Math.max(arr[idx]?arr[idx]:0,td.html($(this).text())[0].offsetWidth);
        })
    });
    $('#gview_jqGrid th').each(function(idx){this.style.width=arr[idx]+'px'});//设置页头单元格宽度
    $('#jqGrid tr:eq(0) td').each(function(idx){this.style.width=arr[idx]+'px'});//设置内容表格中控制单元格宽度的单元格，在第一行

    var total=$('#jqGrid').closest('.ui-jqgrid-bdiv')[0].scrollWidth;//获取表格宽度
    $('#jqGrid,#gview_jqGrid .ui-jqgrid-htable').css({width:total});//设置表头表格和内容表格宽度
    $('#gbox_jqGrid,#gview_jqGrid,#gview_jqGrid>div:gt(0)').css({width:total+15});//设置表头表格和内容表格父容器的宽度
}*/

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            name: null,
            unitName:null,
            firstState:null
        },
		showList: true,
		title: null,
		interview: {}
	},
	methods: {
        passInterview: function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
			var params = new Object();
            params.operation = "passFir";
            params.type = "first";
            params.ids = ids;
            $.ajax({
                type: "POST",
                url: "../interview/updateStatus",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
		},
        failedInterview: function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var params = new Object();
            params.operation = "failedFir";
            params.type = "first";
            params.ids = ids;
            $.ajax({
                type: "POST",
                url: "../interview/updateStatus",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        process: function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            //后台查询所选的id集合是不是都是合格，若不是，提示不合格的，并不继续
            //暂时演示先用前段做验证
            for(var i=0;i<ids.length;i++){
                var rowData = $("#jqGrid").jqGrid('getRowData',ids[i]);
                if(rowData.firInterviewStatus != '合格'){
                    alert(rowData.empname+"不是合格状态!");
                    return;
                }
            }
            //处理通过
            var params = new Object();
            params.operation = "process";
            params.type = "first";
            params.ids = ids;
            $.ajax({
                type: "POST",
                url: "../interview/updateStatus",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });

        },
        jumpSec: function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            //暂时演示先用前段做验证
            for(var i=0;i<ids.length;i++){
                var rowData = $("#jqGrid").jqGrid('getRowData',ids[i]);
                if(rowData.firInterviewStatus != '合格'){
                    alert(rowData.empname+"不是合格状态!");
                    return;
                }
            }

            var params = new Object();
            params.operation = "jumpSec";
            params.type = "first";
            params.ids = ids;
            $.ajax({
                type: "POST",
                url: "../interview/updateStatus",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });

        },
        jumpPS: function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var params = new Object();
            params.operation = "jumpPS";
            params.type = "first";
            params.ids = ids;
            $.ajax({
                type: "POST",
                url: "../interview/updateStatus",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });

        },

		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.interview = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.interview.id == null ? "../interview/save" : "../interview/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.interview),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../interview/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../interview/info/"+id, function(r){
                vm.interview = r.interview;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'empname': vm.q.name,
                          'unitName':vm.q.unitName,
                          'firstState':vm.q.firstState},
                page:page
            }).trigger("reloadGrid");
		}
	}
});